//
//  OutsideWallView.m
//  Model
//
//  Created by zeb-apple on 2017/6/2.
//  Copyright © 2017年 YDJH. All rights reserved.
//  室外后墙

#import "OutsideWallView.h"
#import "HZFurniture.h"
#import "DirectionView.h"
#import "HZFurniture_Header.h"
#import "HZOutsideWall.h"
#import <objc/message.h>

@interface OutsideWallView ()

@property (nonatomic, strong) NSMutableArray *pool; //重用池

@property (nonatomic, strong) DirectionView *top; //上
@property (nonatomic, strong) DirectionView *bottom; //下
@property (nonatomic, strong) DirectionView *left; //左
@property (nonatomic, strong) DirectionView *right; //右

@property (nonatomic, weak) HZOutsideWall *outsideWall;

@property (nonatomic, strong) NSMutableArray *dataSource;

@property (nonatomic, copy) FurnitureClickBlock  furnitureClickBlock;
@property (nonatomic, copy) GoUpBlock upBlock;
@property (nonatomic, copy) GoDownBlock downBlock;
@property (nonatomic, copy) GoLeftBlock leftBlock;
@property (nonatomic, copy) GoRightBlock rightBlock;
@end

@implementation OutsideWallView


- (instancetype)initWithFrame:(CGRect)frame furnitureClickBlock:(FurnitureClickBlock)furnitureClickBlock upBlock:(GoUpBlock)upBlock downBlock:(GoDownBlock)downBlock leftBlock:(GoLeftBlock)leftBlock rightBlock:(GoRightBlock)rightBlock {
    self = [super initWithFrame:frame];
    if (self) {
        self.userInteractionEnabled = YES;
        self.furnitureClickBlock = furnitureClickBlock;
        self.upBlock = upBlock;
        self.downBlock = downBlock;
        self.leftBlock = leftBlock;
        self.rightBlock = rightBlock;
        [self initView];
    }
    return self;
}
- (void)initView {
    
    [self addSubview:self.top];
    [self addSubview:self.left];
    [self addSubview:self.bottom];
    [self addSubview:self.right];
}
#pragma mark
#pragma mark getter
- (NSMutableArray *)pool {
    if (!_pool) {
        _pool = [NSMutableArray array];
    }
    return _pool;
}
- (DirectionView *)top {
    if (!_top) {
        __weak typeof(self) weakSelf = self;
        _top = [[DirectionView alloc] initWithFrame:CGRectZero clickDirectionViewBlock:^(DirectionView *view) {
            weakSelf.upBlock(weakSelf.outsideWall);
        }];
        _top.image = [UIImage imageNamed:@"upArrow"];
    }
    return _top;
}
- (DirectionView *)bottom {
    if (!_bottom) {
        __weak typeof(self) weakSelf = self;
        _bottom = [[DirectionView alloc] initWithFrame:CGRectZero clickDirectionViewBlock:^(DirectionView *view) {
            weakSelf.downBlock(weakSelf.outsideWall);
        }];
        _bottom.image = [UIImage imageNamed:@"downArrow"];
    }
    return _bottom;
}
- (DirectionView *)left {
    if (!_left) {
        __weak typeof(self) weakSelf = self;
        _left = [[DirectionView alloc] initWithFrame:CGRectZero clickDirectionViewBlock:^(DirectionView *view) {
            weakSelf.leftBlock(weakSelf.outsideWall);
        }];
        _left.image = [UIImage imageNamed:@"leftArrow"];
    }
    return _left;
}
- (DirectionView *)right {
    if (!_right) {
        __weak typeof(self) weakSelf = self;
        _right = [[DirectionView alloc] initWithFrame:CGRectZero clickDirectionViewBlock:^(DirectionView *view) {
            weakSelf.rightBlock(weakSelf.outsideWall);
        }];
        _right.image = [UIImage imageNamed:@"rightArrow"];
    }
    return _right;
}
- (void)layoutSubviews {
    [super layoutSubviews];
    
    self.top.frame = CGRectMake(SCREEN_WIDTH/2 - self.top.image.size.width/2, 0, self.top.image.size.width, self.top.image.size.height);
    
    self.left.frame = CGRectMake(0, CGRectGetHeight(self.frame)/2 - self.left.image.size.height/2, self.left.image.size.width, self.left.image.size.height);
    
    self.bottom.frame = CGRectMake(SCREEN_WIDTH/2 - self.bottom.image.size.width/2, CGRectGetHeight(self.frame) - self.bottom.image.size.height, self.bottom.image.size.width, self.bottom.image.size.height);
    
    self.right.frame = CGRectMake(SCREEN_WIDTH - self.right.image.size.width, CGRectGetHeight(self.frame)/2 - self.right.image.size.height/2, self.right.image.size.width, self.right.image.size.height);
}
//刷新视图
- (void)reloadWithOutsideWall:(HZOutsideWall *)outsideWall {
    
    self.outsideWall = outsideWall;
    self.top.hidden = !outsideWall.canUp;
    self.left.hidden = !outsideWall.canLeft;
    self.bottom.hidden = !outsideWall.canDown;
    self.right.hidden = !outsideWall.canRight;
    
    __weak typeof(self) weakSelf = self;
    //重置家具视图
    [self resetSubviewsWithClass:[OutsideFurnitureView class]];
    //根据数据源家具model添加家具
    for (HZFurniture *furniture in outsideWall.furnitureArr) {
        
        OutsideFurnitureView *furnitureView = (OutsideFurnitureView *)[self goBackViewFromPoolWithClass:[OutsideFurnitureView class]];
        
        furnitureView.furniture = furniture;
        furnitureView.furnitureClickBlock = ^(HZFurniture *furniture, UIImageView *superView) {
            weakSelf.furnitureClickBlock(furniture, superView);
        };
        [self addSubview:furnitureView];
    }
}
// 根据class返回未使用的view
- (UIView *)goBackViewFromPoolWithClass:(Class)class {
    
    __block UIView *view = nil;
    [self.pool enumerateObjectsUsingBlock:^(UIView * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        if ([obj isKindOfClass:class] && !obj.haveuse) {
            *stop = YES;
            view = obj;
            view.haveuse = YES;
        }
        
    }];
    if (!view) {
        view = [[class alloc] init];
        view.haveuse = YES;
        [self addSubview:view];
        //新增加的添加到缓存池
        [self.pool addObject:view];
    }
    return view;
}
//将子视图的 haveuse 重置
- (void)resetSubviewsWithClass:(Class)class {
    
    for (UIView *view in self.subviews) {
        if ([view isKindOfClass:class]) {
            view.haveuse = NO;
            [view removeFromSuperview];
        }
    }
}


/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end




@implementation UIView (Room_Property)

- (void)setHaveuse:(BOOL)haveuse {
    // 动态添加属性的本质是:让对象的某个属性与值产生关联
    objc_setAssociatedObject(self, @selector(haveuse), @(haveuse), OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}
- (BOOL)haveuse {
    // 获取对应属性的值
    return [objc_getAssociatedObject(self, @selector(haveuse)) boolValue];
}

@end

